<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="renderer" content="webkit" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  <title>千锋教育大前端React教学</title>
  <!-- 引入react -->
  <script src="../../lib/js/react.js"></script>
  <!-- 引入react-dom -->
  <script src="../../lib/js/react-dom.js"></script>
  <!-- 引入babel 用来解析jsx -->
  <script src="../../lib/js/babel.js"></script>
</head>

<body style="margin:100px;font-size:22px;">
  <!-- 定义一个容器 -->
  <div id="app"></div>
  <script type="text/babel"> /*此处一定要写babel*/
    const SurnameContext = React.createContext({ surname: '孙' }); // 参数是默认值
    class Parent extends React.Component {
      render() {
        return (
          <div>
            <div>我是第一代：</div>
            <SurnameContext.Consumer>
              {value => <div>我姓：{value.surname}</div>}
            </SurnameContext.Consumer>
            <Child name="朱大肠" />
            <Child name="朱小明" />
          </div>
        )
      }
    }
    class Child extends React.Component {
      render() {
        const { name } = this.props;
        return (
          <div>
            <div>我是第二代：</div>
            <SurnameContext.Consumer>
              {value => <div>我叫：{name}、我姓：{value.surname}</div>}
            </SurnameContext.Consumer>
          </div>
        )
      }
    }
    // 使用js函数返回一个组件
    function App() {
      return (
        <SurnameContext.Provider value={{
          surname: '朱', // 姓
        }}>
          <Parent />
        </SurnameContext.Provider>
      );
    }
    // 挂载应用
    ReactDOM.createRoot(document.getElementById('app')).render(<App />);
  </script>
</body>

</html>